import pagesJson from '@/pages.json'

function getRoutes() {
    let pages = pagesJson.pages.map(item => item.path)
    let subPages = pagesJson.subPackages.map(item1 => {
        return item1.pages.map(item2 => {
            return `${item1.root}/${item2.path}`
        })
    }).flat(1)
    return [...pages, ...subPages]
}

function getRoute(routeName) {
    let routes = getRoutes()
    let route = routes.find(item => item.endsWith(routeName))
    return `/${route}`
}

function getParam(param) {
    return `?param=${JSON.stringify(param)}`
}

export function xtPush(routeName, param) {
    let prefix = getRoute(routeName)
    uni.navigateTo({
        url: `${prefix}${getParam(param)}`
    })
}

export function xtReplace(routeName, param) {
    let prefix = getRoute(routeName)
    uni.redirectTo({
        url: `${prefix}${getParam(param)}`
    })
}

export function xtGo(count) {
    uni.navigateBack({
        delta: count
    })
}

export function xtSwitchTab(routeName, param) {
    let prefix = getRoute(routeName)
    uni.reLaunch({
        url: `${prefix}${getParam(param)}`
    })
}